{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于矩阵的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "#import sys\n",
    "#reload(sys)\n",
    "#sys.setdefaultencoding(\"utf-8\")\n",
    "###以上代码适用于python2，Python3替换成如下的代码：\n",
    "import importlib\n",
    "import sys\n",
    "importlib.reload(sys)\n",
    "#sys.setdefaultencoding(\"utf-8\")  ###Python3字符串默认编码unicode, 所以sys.setdefaultencoding也不存在了\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import pickle\n",
    "import scipy.io as sio\n",
    "\n",
    "#距离\n",
    "import scipy.spatial.distance as ssd\n",
    "\n",
    "from numpy.random import random  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 从文件读入输入，初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 读入数据做初始化\n",
    "    \n",
    "#用户和item的索引\n",
    "user_index = pickle.load(open(\"user_index.pkl\", 'rb'))\n",
    "item_index = pickle.load(open(\"item_index.pkl\", 'rb'))\n",
    "\n",
    "n_users = len(user_index)\n",
    "n_items = len(item_index)\n",
    "    \n",
    "#用户-物品关系矩阵R\n",
    "#user_item_scores = sio.mmread(\"user_items_scores\").todense()\n",
    "    \n",
    "#倒排表\n",
    "##每个用户播放的歌曲\n",
    "user_items = pickle.load(open(\"user_items.pkl\", 'rb'))\n",
    "##事件参加的用户\n",
    "item_users = pickle.load(open(\"item_users.pkl\", 'rb'))\n",
    "\n",
    "#所有item之间的相似度\n",
    "#similarity_matrix = pickle.load(open(\"items_similarity.pkl\", 'rb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(211686, 9)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('triplet_dataset_sub_song_merged.csv')\n",
    "\n",
    "#每个用户的总播放次数\n",
    "triplet_train_sum_df = train[['user','listen_count']].groupby('user').sum().reset_index()\n",
    "triplet_train_sum_df.rename(columns={'listen_count':'total_listen_count'},inplace=True)\n",
    "\n",
    "#每首歌曲的播放比例\n",
    "train = pd.merge(train,triplet_train_sum_df)\n",
    "train['fractional_play_count'] = train['listen_count']/train['total_listen_count']\n",
    "del triplet_train_sum_df\n",
    "\n",
    "song_count_df = pd.read_csv('song_playcount_df.csv')\n",
    "song_count_subset = song_count_df.head(n=1000)\n",
    "song_subset = list(song_count_subset.song)\n",
    "trian_sub = train[train.song.isin(song_subset)]\n",
    "\n",
    "del train\n",
    "\n",
    "trian_sub.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "      <th>total_listen_count</th>\n",
       "      <th>fractional_play_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "      <td>861</td>\n",
       "      <td>0.001161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAJJDS12A8C13A3FB</td>\n",
       "      <td>1</td>\n",
       "      <td>I Got Mine</td>\n",
       "      <td>Attack &amp; Release</td>\n",
       "      <td>The Black Keys</td>\n",
       "      <td>2008</td>\n",
       "      <td>861</td>\n",
       "      <td>0.001161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAMPRJ12A8AE45F38</td>\n",
       "      <td>20</td>\n",
       "      <td>Rorol</td>\n",
       "      <td>Identification Parade</td>\n",
       "      <td>Octopus Project</td>\n",
       "      <td>2002</td>\n",
       "      <td>861</td>\n",
       "      <td>0.023229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOCAFDI12A8C13D10E</td>\n",
       "      <td>4</td>\n",
       "      <td>TTHHEE PPAARRTTYY</td>\n",
       "      <td>Justice</td>\n",
       "      <td>Justice</td>\n",
       "      <td>0</td>\n",
       "      <td>861</td>\n",
       "      <td>0.004646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOCJCVE12A8C13CDDB</td>\n",
       "      <td>11</td>\n",
       "      <td>One Minute To Midnight</td>\n",
       "      <td>Justice</td>\n",
       "      <td>Justice</td>\n",
       "      <td>0</td>\n",
       "      <td>861</td>\n",
       "      <td>0.012776</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        user                song  \\\n",
       "0   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAFTRR12AF72A8D4D   \n",
       "2   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAJJDS12A8C13A3FB   \n",
       "4   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAMPRJ12A8AE45F38   \n",
       "15  5a905f000fc1ff3df7ca807d57edb608863db05d  SOCAFDI12A8C13D10E   \n",
       "18  5a905f000fc1ff3df7ca807d57edb608863db05d  SOCJCVE12A8C13CDDB   \n",
       "\n",
       "    listen_count                          title                release  \\\n",
       "0              1  Harder Better Faster Stronger              Discovery   \n",
       "2              1                     I Got Mine       Attack & Release   \n",
       "4             20                          Rorol  Identification Parade   \n",
       "15             4              TTHHEE PPAARRTTYY                Justice   \n",
       "18            11         One Minute To Midnight                Justice   \n",
       "\n",
       "        artist_name  year  total_listen_count  fractional_play_count  \n",
       "0         Daft Punk  2007                 861               0.001161  \n",
       "2    The Black Keys  2008                 861               0.001161  \n",
       "4   Octopus Project  2002                 861               0.023229  \n",
       "15          Justice     0                 861               0.004646  \n",
       "18          Justice     0                 861               0.012776  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trian_sub.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化模型参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "K = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据训练数据训练模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#init parameters\n",
    "#bias\n",
    "bi = np.zeros(n_items)  \n",
    "bu = np.zeros(n_users)  \n",
    "    \n",
    "#the small matrix\n",
    "P = random((n_users,K))/10*(np.sqrt(K))\n",
    "Q = random((K, n_items))/10*(np.sqrt(K))  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读入训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n_samples = trian_sub.shape[0]\n",
    "\n",
    "mu = 0.0\n",
    "uids = []  #每条记录的用户索引\n",
    "i_ids = [] #每条记录的item索引\n",
    "\n",
    "#用户-Item关系矩阵R，临时变量，训练完了R不再需要\n",
    "R= np.matrix(np.zeros(shape=(n_users, n_items)), float)\n",
    "    \n",
    "for i in range(n_samples):  \n",
    "    uid = user_index[trian_sub.iloc[i]['user']]  #用户\n",
    "    iid = item_index[trian_sub.iloc[i]['song']] #歌曲\n",
    "        \n",
    "    uids.append(uid)\n",
    "    i_ids.append(iid)\n",
    "        \n",
    "    R[uid,iid] = trian_sub.iloc[i]['fractional_play_count'] #interested\n",
    "    mu += R[uid,iid]\n",
    "\n",
    "mu /= n_samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 迭代，随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#根据当前参数，预测用户uid对Item（i_id）的打分     \n",
    "def pred_SVD(uid, i_id):\n",
    "    score = mu + bi[i_id] + bu[uid] + np.dot(P[uid,:], Q[:,i_id])  \n",
    "    return score    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the  0 -th  step is running\n",
      "the rmse of the 0 th step on train data is:2174.994214698128\n",
      "the  1 -th  step is running\n",
      "the rmse of the 1 th step on train data is:127.60652588731546\n",
      "the  2 -th  step is running\n",
      "the rmse of the 2 th step on train data is:121.20385790833946\n",
      "the  3 -th  step is running\n",
      "the rmse of the 3 th step on train data is:118.50826882097614\n",
      "the  4 -th  step is running\n",
      "the rmse of the 4 th step on train data is:116.68190643672176\n",
      "the  5 -th  step is running\n",
      "the rmse of the 5 th step on train data is:115.34693287498517\n",
      "the  6 -th  step is running\n",
      "the rmse of the 6 th step on train data is:114.30699149522114\n",
      "the  7 -th  step is running\n",
      "the rmse of the 7 th step on train data is:113.53485273419359\n",
      "the  8 -th  step is running\n",
      "the rmse of the 8 th step on train data is:112.68863589819344\n",
      "the  9 -th  step is running\n",
      "the rmse of the 9 th step on train data is:112.08586256145529\n",
      "the  10 -th  step is running\n",
      "the rmse of the 10 th step on train data is:111.55449540169408\n",
      "the  11 -th  step is running\n",
      "the rmse of the 11 th step on train data is:111.06648615663477\n",
      "the  12 -th  step is running\n",
      "the rmse of the 12 th step on train data is:110.68393998727808\n",
      "the  13 -th  step is running\n",
      "the rmse of the 13 th step on train data is:110.31079125098653\n",
      "the  14 -th  step is running\n",
      "the rmse of the 14 th step on train data is:110.04705635433608\n",
      "the  15 -th  step is running\n",
      "the rmse of the 15 th step on train data is:109.7223388166271\n",
      "the  16 -th  step is running\n",
      "the rmse of the 16 th step on train data is:109.40828651038204\n",
      "the  17 -th  step is running\n",
      "the rmse of the 17 th step on train data is:109.15232589716378\n",
      "the  18 -th  step is running\n",
      "the rmse of the 18 th step on train data is:108.94705140174449\n",
      "the  19 -th  step is running\n",
      "the rmse of the 19 th step on train data is:108.73348956047738\n",
      "the  20 -th  step is running\n",
      "the rmse of the 20 th step on train data is:108.53675999538534\n",
      "the  21 -th  step is running\n",
      "the rmse of the 21 th step on train data is:108.33488277559366\n",
      "the  22 -th  step is running\n",
      "the rmse of the 22 th step on train data is:108.18721916471857\n",
      "the  23 -th  step is running\n",
      "the rmse of the 23 th step on train data is:108.03189003913903\n",
      "the  24 -th  step is running\n",
      "the rmse of the 24 th step on train data is:107.91987809759826\n",
      "the  25 -th  step is running\n",
      "the rmse of the 25 th step on train data is:107.78205286502795\n",
      "the  26 -th  step is running\n",
      "the rmse of the 26 th step on train data is:107.68369403084593\n",
      "the  27 -th  step is running\n",
      "the rmse of the 27 th step on train data is:107.55709177406317\n",
      "the  28 -th  step is running\n",
      "the rmse of the 28 th step on train data is:107.45722397110708\n",
      "the  29 -th  step is running\n",
      "the rmse of the 29 th step on train data is:107.37612164798969\n",
      "the  30 -th  step is running\n",
      "the rmse of the 30 th step on train data is:107.28865335381319\n",
      "the  31 -th  step is running\n",
      "the rmse of the 31 th step on train data is:107.19147225908567\n",
      "the  32 -th  step is running\n",
      "the rmse of the 32 th step on train data is:107.11185029052604\n",
      "the  33 -th  step is running\n",
      "the rmse of the 33 th step on train data is:107.04439173402982\n",
      "the  34 -th  step is running\n",
      "the rmse of the 34 th step on train data is:106.96347824922476\n",
      "the  35 -th  step is running\n",
      "the rmse of the 35 th step on train data is:106.92464312224412\n",
      "the  36 -th  step is running\n",
      "the rmse of the 36 th step on train data is:106.8527047619813\n",
      "the  37 -th  step is running\n",
      "the rmse of the 37 th step on train data is:106.80105194147293\n",
      "the  38 -th  step is running\n",
      "the rmse of the 38 th step on train data is:106.75900101670764\n",
      "the  39 -th  step is running\n",
      "the rmse of the 39 th step on train data is:106.70749118281675\n",
      "the  40 -th  step is running\n",
      "the rmse of the 40 th step on train data is:106.67282588623777\n",
      "the  41 -th  step is running\n",
      "the rmse of the 41 th step on train data is:106.62940810178684\n",
      "the  42 -th  step is running\n",
      "the rmse of the 42 th step on train data is:106.59327162802077\n",
      "the  43 -th  step is running\n",
      "the rmse of the 43 th step on train data is:106.56076913027475\n",
      "the  44 -th  step is running\n",
      "the rmse of the 44 th step on train data is:106.52549268177945\n",
      "the  45 -th  step is running\n",
      "the rmse of the 45 th step on train data is:106.50133046956492\n",
      "the  46 -th  step is running\n",
      "the rmse of the 46 th step on train data is:106.46819410499982\n",
      "the  47 -th  step is running\n",
      "the rmse of the 47 th step on train data is:106.44093653012585\n",
      "the  48 -th  step is running\n",
      "the rmse of the 48 th step on train data is:106.42029570704597\n",
      "the  49 -th  step is running\n",
      "the rmse of the 49 th step on train data is:106.39210778108723\n"
     ]
    }
   ],
   "source": [
    "n_steps = 50\n",
    "gamma=0.04\n",
    "Lambda=0.15\n",
    "\n",
    "for step in range(n_steps):  \n",
    "    print ('the ',step,'-th  step is running'  )\n",
    "    \n",
    "    rmse_sum=0.0 \n",
    "            \n",
    "    #将训练样本打散顺序\n",
    "    kk = np.random.permutation(n_samples)  \n",
    "    for j in range(n_samples):  \n",
    "        #每次一个训练样本\n",
    "        index = kk[j]  \n",
    " \n",
    "        uid = uids[index]\n",
    "        iid = i_ids[index]\n",
    " \n",
    "        #预测残差\n",
    "        eui = R[uid,iid] - pred_SVD(uid,iid)\n",
    "        \n",
    "        #残差平方和\n",
    "        rmse_sum += eui**2\n",
    "               \n",
    "        #随机梯度下降，更新\n",
    "        bu[uid]+= gamma*(eui - Lambda*bu[uid])  \n",
    "        bi[iid]+= gamma*(eui - Lambda*bi[iid]) \n",
    "            \n",
    "        for k in range(K):\n",
    "            #P,Q 同时更新，temp暂存P的更新之的值\n",
    "            temp = P[uid,k] + gamma * eui * Q[k,iid] - Lambda * P[uid,k]\n",
    "               \n",
    "            Q[k,iid] += gamma * eui * P[uid,k] - Lambda * Q[k,iid]\n",
    "            P[uid,k] = temp\n",
    "                \n",
    "    #学习率递减\n",
    "    gamma=gamma*0.93  \n",
    "    print(\"the rmse of the {} th step on train data is:{}\".format(step, rmse_sum))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算用户对每个item的预测打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cur_user = '5a905f000fc1ff3df7ca807d57edb608863db05d'\n",
    "\n",
    "cur_user_id = user_index[cur_user]\n",
    "cur_user_items = user_items[cur_user_id]\n",
    "\n",
    "sim_accumulate=0.0  \n",
    "rat_acc=0.0 \n",
    "\n",
    "user_items_scores = np.zeros(n_items)\n",
    "\n",
    "for i in range(n_items):  # all items of user not played\n",
    "    if i not in cur_user_items:\n",
    "        user_items_scores[i] = pred_SVD(cur_user_id, i)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  2.33218627e-02,   1.48145861e-02,   1.39887667e-02,\n",
       "         6.90780285e-03,   1.18968878e-02,   8.23777542e-03,\n",
       "         7.77169770e-03,   1.40678079e-02,   1.33173769e-02,\n",
       "         1.35500216e-02,   1.15987917e-02,   1.06604945e-02,\n",
       "         9.95018887e-03,   5.90695309e-03,   4.97504930e-03,\n",
       "         0.00000000e+00,   5.21719000e-03,   6.63668438e-03,\n",
       "         1.20074202e-02,   3.28306980e-03,   5.08068143e-03,\n",
       "         1.08337546e-02,   5.32964064e-03,   1.05317308e-02,\n",
       "         9.15599876e-03,   9.82008202e-03,   0.00000000e+00,\n",
       "         6.40899183e-03,   4.28848813e-03,   5.13553591e-03,\n",
       "         6.84664747e-03,   8.58467861e-03,   0.00000000e+00,\n",
       "         1.02779794e-02,   1.27642287e-02,   4.17503064e-03,\n",
       "         0.00000000e+00,   4.53815183e-03,   4.92469612e-03,\n",
       "         0.00000000e+00,   8.69348761e-03,   3.88178229e-03,\n",
       "         4.73310697e-03,   1.29157810e-02,   1.10443240e-02,\n",
       "         1.04351891e-02,   9.19805005e-03,   4.39486795e-03,\n",
       "         7.49517575e-03,   1.12640943e-02,   7.29733701e-03,\n",
       "         4.29685099e-03,   3.48767338e-03,   4.49796668e-03,\n",
       "         9.13638433e-03,   6.26913237e-03,   9.25391991e-03,\n",
       "         4.80372988e-03,   5.23740841e-03,   1.11854287e-02,\n",
       "         4.63576159e-03,   7.96434390e-03,   9.91601013e-03,\n",
       "         4.27223253e-03,   4.28529977e-03,   4.91363121e-03,\n",
       "         4.85627287e-03,   8.64411793e-03,   0.00000000e+00,\n",
       "         4.16726192e-03,   5.05901806e-03,   4.08021183e-03,\n",
       "         5.14319328e-03,   0.00000000e+00,   3.99011600e-03,\n",
       "         5.29333073e-03,   4.83999043e-03,   4.69178733e-03,\n",
       "         6.34221366e-03,   4.37146233e-03,   8.08251654e-03,\n",
       "         4.59429048e-03,   4.13417708e-03,   6.09970773e-03,\n",
       "         3.90462979e-03,   0.00000000e+00,   4.04068388e-03,\n",
       "         5.37820075e-03,   4.50184477e-03,   3.77906588e-03,\n",
       "         1.20079167e-02,   4.09021845e-03,   4.33481469e-03,\n",
       "         9.68760548e-03,   2.09771468e-02,   5.93981784e-03,\n",
       "         3.36227148e-03,   4.85821562e-03,   4.88842492e-03,\n",
       "         4.50352772e-03,   4.13174059e-03,   1.91846349e-02,\n",
       "         3.71953720e-03,   3.83396614e-03,   0.00000000e+00,\n",
       "         4.57792650e-03,   3.41080566e-03,   2.91717749e-03,\n",
       "         0.00000000e+00,   1.20894823e-02,   4.07723272e-03,\n",
       "         3.92553530e-03,   1.22796043e-02,   4.31752586e-03,\n",
       "         6.36314181e-03,   3.74450083e-03,   4.29690205e-03,\n",
       "         5.67074477e-02,   1.57533782e-02,   6.81052823e-03,\n",
       "         3.89134881e-03,   9.89954028e-03,   1.15582725e-02,\n",
       "         2.83282027e-03,   4.89126208e-03,   4.83174321e-03,\n",
       "         5.31362703e-03,   6.65085974e-03,   6.00095416e-03,\n",
       "         3.74369671e-03,   1.42501277e-03,   1.14079490e-02,\n",
       "         5.85800901e-03,   4.06836680e-03,   1.76458607e-03,\n",
       "         1.86152887e-02,   1.32983017e-02,   3.95513736e-03,\n",
       "         7.58787542e-03,   5.58203971e-03,   3.67717509e-03,\n",
       "         6.20433109e-03,   3.64759124e-03,   3.87430738e-03,\n",
       "         3.54891828e-03,   4.30151341e-03,   8.34834917e-03,\n",
       "         5.27807255e-03,   9.40637104e-03,   4.38660305e-03,\n",
       "         9.97383683e-03,   9.52033008e-03,   1.16896274e-02,\n",
       "         3.99525440e-03,   6.15239885e-03,   1.45820480e-02,\n",
       "         9.01384415e-03,   5.10502559e-03,   4.86347828e-03,\n",
       "         4.04351235e-03,   4.82697601e-03,   4.36459036e-03,\n",
       "         4.97677027e-03,   3.32926763e-03,   6.12587477e-03,\n",
       "         3.56182151e-03,   3.69043239e-03,   4.16569308e-03,\n",
       "         1.51261370e-02,   1.41777228e-02,   4.34042832e-03,\n",
       "         1.35824264e-02,   4.77411779e-03,   4.98645070e-03,\n",
       "         0.00000000e+00,   1.11537222e-02,   3.41174095e-03,\n",
       "         4.07626751e-03,   1.42872420e-02,   4.14518393e-03,\n",
       "         4.32465108e-03,   4.60500338e-03,   7.08647297e-03,\n",
       "         1.00013244e-02,   5.42075394e-03,   3.15449717e-03,\n",
       "         5.49910635e-03,   4.96592671e-03,   1.08457609e-02,\n",
       "         0.00000000e+00,   1.70299226e-02,   0.00000000e+00,\n",
       "         3.08661247e-03,   9.01243015e-03,   5.05667438e-03,\n",
       "         1.56871524e-02,   2.35534877e-03,   3.32941926e-03,\n",
       "         3.61456889e-03,   8.48583367e-03,   7.03208893e-03,\n",
       "         0.00000000e+00,   5.74776007e-03,   3.88608946e-03,\n",
       "         4.41631237e-03,   5.07181531e-03,   4.44009850e-03,\n",
       "         3.05983490e-03,   4.59775157e-03,   1.19274031e-02,\n",
       "         5.68251640e-03,   2.05638235e-02,   1.21790651e-02,\n",
       "         1.99852583e-02,   2.87702399e-02,   1.04372741e-02,\n",
       "         1.44765829e-02,   5.34215616e-03,   9.42531844e-03,\n",
       "         4.87363972e-03,   1.12811341e-02,   1.24602993e-02,\n",
       "         3.74116342e-03,   3.61419314e-03,   4.46921699e-03,\n",
       "         5.63790489e-03,   9.70543201e-03,   5.16214101e-03,\n",
       "         9.45591197e-03,   5.99430293e-03,   3.80680844e-03,\n",
       "         1.01726551e-02,   4.51291847e-03,   3.12959728e-03,\n",
       "         1.26660452e-02,   1.47296092e-02,   1.25794184e-02,\n",
       "         0.00000000e+00,   1.08971325e-02,   7.95462422e-03,\n",
       "         1.69885139e-02,   3.65858062e-03,   9.92405985e-03,\n",
       "         4.59352468e-03,   1.22376441e-02,   9.31282584e-03,\n",
       "         7.20594129e-03,   3.44055344e-03,   3.50335626e-03,\n",
       "         5.16786184e-03,   5.91996672e-03,   3.74613372e-03,\n",
       "         1.22551660e-02,   5.73838724e-03,   4.87950054e-03,\n",
       "         0.00000000e+00,   2.67506722e-03,   3.84788733e-03,\n",
       "         1.40686696e-02,   5.37155029e-03,   1.20562248e-02,\n",
       "         4.46114482e-03,   8.03355831e-03,   4.84778839e-03,\n",
       "         7.50134608e-03,   6.08914399e-03,   1.31048884e-02,\n",
       "         5.88213720e-03,   7.24857607e-03,   2.17218864e-02,\n",
       "         1.94373103e-02,   5.28224481e-03,   1.70540566e-02,\n",
       "         7.46643019e-03,   1.14471140e-02,   7.44299545e-03,\n",
       "         2.52787394e-03,   4.90875731e-03,   2.50664474e-03,\n",
       "         4.85067483e-03,   7.27253778e-03,   1.00878640e-02,\n",
       "         5.84248678e-02,   2.27686461e-03,   9.50000669e-03,\n",
       "         7.30547022e-03,   7.33678846e-03,   1.47814687e-02,\n",
       "         4.26953202e-03,   4.24402070e-03,   3.26905455e-03,\n",
       "         1.04829028e-02,   1.01131270e-02,   3.77893138e-03,\n",
       "         5.30825548e-03,   2.55117714e-02,   9.41040107e-03,\n",
       "         3.73541440e-03,   7.60351186e-03,   1.44012796e-02,\n",
       "         3.09471457e-03,   1.48627983e-02,   3.53774134e-03,\n",
       "         4.30305509e-03,   1.23876805e-02,   5.48565072e-03,\n",
       "         1.02974335e-02,   5.23281108e-03,   2.11255316e-03,\n",
       "         1.19105194e-02,   3.34285871e-03,   2.62867337e-02,\n",
       "         6.45940838e-03,   3.03903159e-02,   4.81102229e-03,\n",
       "         9.17834023e-03,   5.73986845e-03,   5.42424092e-03,\n",
       "         8.24300442e-03,   4.20289352e-03,   8.36914697e-03,\n",
       "         6.37598645e-03,   0.00000000e+00,   1.03054337e-02,\n",
       "         1.00316150e-02,   9.72249446e-03,   6.16987823e-03,\n",
       "         6.66486922e-03,   6.35163461e-03,   3.80701807e-03,\n",
       "         5.75523989e-03,   5.73511583e-03,   4.13453736e-03,\n",
       "         4.19930642e-03,   3.37427210e-02,   0.00000000e+00,\n",
       "         5.83178670e-03,   5.58937033e-03,   8.57621695e-03,\n",
       "         3.38822286e-03,   4.97851209e-03,   3.89849337e-03,\n",
       "         9.54351614e-03,   3.24863840e-02,   5.09950514e-03,\n",
       "         4.60027332e-03,   3.61873904e-03,   5.05314363e-03,\n",
       "         9.39496043e-03,   1.33871736e-02,   6.92133528e-03,\n",
       "         5.63294854e-03,   1.21298932e-02,   1.10855739e-02,\n",
       "         5.04909226e-03,   0.00000000e+00,   1.12736470e-02,\n",
       "         9.65783658e-03,   1.05229433e-02,   3.70235209e-03,\n",
       "         6.17679691e-03,   7.02855866e-03,   5.76755181e-03,\n",
       "         3.40382901e-03,   5.40939703e-03,   7.72053729e-03,\n",
       "         1.52177441e-02,   3.93300950e-03,   7.81839406e-03,\n",
       "         3.94496549e-03,   4.11326184e-03,   1.48322513e-02,\n",
       "         1.07332945e-02,   6.41897524e-03,   5.00376394e-03,\n",
       "         0.00000000e+00,   6.41394166e-03,   9.44957077e-03,\n",
       "         2.92243699e-02,   3.66203147e-03,   1.38905371e-02,\n",
       "         9.34569381e-03,   4.08376426e-03,   1.45852169e-02,\n",
       "         4.69627437e-03,   5.79228331e-03,   4.56351363e-03,\n",
       "         5.58056864e-03,   1.26780603e-02,   4.31135999e-03,\n",
       "         5.33450997e-03,   6.83160432e-03,   1.10669544e-02,\n",
       "         7.23358130e-03,   4.48138447e-03,   7.19337234e-03,\n",
       "         7.32940737e-03,   3.93876752e-03,   1.43472511e-02,\n",
       "         1.96952578e-02,   0.00000000e+00,   4.43617676e-03,\n",
       "         4.24830551e-03,   3.35937583e-03,   1.04216705e-02,\n",
       "         6.82580229e-03,   5.18000949e-03,   0.00000000e+00,\n",
       "         4.01744561e-03,   5.43897466e-03,   6.90684079e-03,\n",
       "         5.65268226e-03,   1.75163324e-03,   3.48967980e-03,\n",
       "         4.86454809e-03,   6.28685313e-03,   2.06536232e-02,\n",
       "         5.27384128e-03,   8.13167618e-03,   2.71689154e-04,\n",
       "         4.98907147e-03,   7.16842258e-03,   5.41225414e-03,\n",
       "         0.00000000e+00,   9.14583233e-03,   6.00818493e-03,\n",
       "         5.39884067e-03,   8.78326798e-03,   8.56191749e-03,\n",
       "         1.03465456e-02,   3.06375800e-03,   3.38622599e-03,\n",
       "         4.37719564e-03,   1.10053360e-02,   1.33862303e-02,\n",
       "         6.89931428e-03,   4.62015082e-03,   1.36526747e-02,\n",
       "         5.30944713e-03,   6.22546559e-03,   9.64747396e-03,\n",
       "         1.18698049e-02,   5.62411304e-03,   3.64464795e-03,\n",
       "         5.19773593e-03,   8.33377574e-03,   3.54655071e-03,\n",
       "         4.90174712e-03,   2.67191478e-03,   3.83776787e-03,\n",
       "         6.61165328e-03,   0.00000000e+00,   2.52372565e-03,\n",
       "         6.50648972e-03,   2.70623666e-03,   1.33220484e-02,\n",
       "         5.02357019e-03,   7.26911800e-03,   7.56187156e-03,\n",
       "         4.07801799e-03,   1.90532008e-02,   5.94596976e-03,\n",
       "         1.11159499e-02,   2.02075043e-02,   9.63263411e-03,\n",
       "         4.20139564e-03,   9.09372993e-03,   6.88727698e-03,\n",
       "         5.83487379e-03,   6.91386318e-03,   2.67561051e-03,\n",
       "         3.88337916e-03,   5.04620084e-03,   0.00000000e+00,\n",
       "         5.01706180e-03,   4.55420759e-03,   0.00000000e+00,\n",
       "         6.76562963e-02,   5.24021971e-03,   4.02646137e-03,\n",
       "         5.24551728e-03,   4.68362016e-03,   6.53212992e-03,\n",
       "         6.68015689e-03,   4.72842856e-03,   6.04661077e-03,\n",
       "         1.89198299e-02,   7.53541558e-03,   7.36370454e-03,\n",
       "         4.10361473e-03,   4.40685230e-03,   3.72422122e-03,\n",
       "         8.59964701e-03,   4.72251724e-03,   6.02942049e-03,\n",
       "         6.92021485e-03,   6.73217857e-03,   1.01273403e-02,\n",
       "         1.60634615e-02,   3.73571533e-02,   3.32410346e-03,\n",
       "         1.54828575e-02,   1.04225980e-02,   5.46307290e-03,\n",
       "         5.10256737e-03,   7.01508280e-03,   4.50931427e-03,\n",
       "         7.78004251e-03,   5.39418056e-03,   2.44444356e-03,\n",
       "         4.29110352e-03,   1.20605297e-02,   1.19407183e-02,\n",
       "         2.29656251e-03,   6.99580074e-03,   5.54148682e-03,\n",
       "         0.00000000e+00,   3.36104551e-03,   1.16974366e-02,\n",
       "         0.00000000e+00,   2.55339067e-03,   0.00000000e+00,\n",
       "         4.92240231e-03,   0.00000000e+00,   4.39307926e-03,\n",
       "         1.15388559e-02,   0.00000000e+00,   4.87011052e-02,\n",
       "         3.02258397e-03,   5.30070564e-03,   5.79588730e-03,\n",
       "        -1.66754978e-04,   8.41342723e-03,   4.08660052e-03,\n",
       "         1.51789449e-02,   6.65677405e-03,   1.03599434e-02,\n",
       "         8.07438492e-03,   7.77508062e-03,   7.57601714e-03,\n",
       "         1.21281217e-02,   1.03019150e-03,   6.91182023e-03,\n",
       "         4.34353906e-03,   2.05987964e-02,   5.15696526e-03,\n",
       "         4.55435882e-03,   0.00000000e+00,   6.76275229e-03,\n",
       "         1.97729032e-02,   2.51124237e-02,   5.12794054e-03,\n",
       "         1.72322493e-02,   8.34147691e-03,   1.06569673e-02,\n",
       "         6.61280134e-03,   3.57195636e-03,   1.07041707e-02,\n",
       "         3.69032672e-03,   3.17273523e-03,   9.54009975e-03,\n",
       "         6.39700058e-03,   9.72386722e-03,   1.33813693e-02,\n",
       "         5.42507936e-03,   4.53810716e-03,   0.00000000e+00,\n",
       "         6.06453809e-03,   4.96775047e-03,   3.56116414e-03,\n",
       "         3.62674807e-03,   1.12500577e-02,   2.89731560e-03,\n",
       "         1.09974316e-02,   6.24199096e-03,   4.33053786e-03,\n",
       "         0.00000000e+00,   4.71842298e-03,   1.36564444e-02,\n",
       "         8.72249587e-03,   0.00000000e+00,   5.17491043e-03,\n",
       "         7.88044707e-03,   5.61990300e-03,   8.00063043e-03,\n",
       "         6.94494959e-03,   4.59593267e-03,   4.95504433e-03,\n",
       "         0.00000000e+00,   3.94757075e-03,   3.47240041e-03,\n",
       "         6.24061340e-03,   1.13345371e-02,   2.98320795e-02,\n",
       "         4.41512785e-03,   5.05182458e-03,   4.54194966e-03,\n",
       "         4.39263988e-03,   1.95596704e-02,   6.38761630e-03,\n",
       "         2.83765726e-03,   1.36196048e-02,   3.66566940e-03,\n",
       "         5.87250843e-03,   5.30841389e-03,   4.61156969e-03,\n",
       "         9.02730001e-03,   0.00000000e+00,   1.02586550e-02,\n",
       "         3.54033276e-03,   1.06442886e-02,   8.11003649e-03,\n",
       "         6.54025431e-04,   8.92001629e-03,   4.41806966e-03,\n",
       "         1.05161434e-02,   1.69154085e-02,   1.23394415e-02,\n",
       "         1.00606964e-02,   6.42094099e-03,   2.54248340e-02,\n",
       "         9.54263693e-03,   3.33188531e-03,   6.47533015e-03,\n",
       "         9.59656311e-03,   7.87870090e-03,   1.11519584e-02,\n",
       "         1.02872163e-02,   4.60651081e-03,   2.82973191e-02,\n",
       "         2.55466397e-02,   6.14662412e-03,   3.31407477e-03,\n",
       "         4.71681919e-03,   3.69142954e-03,   4.99069278e-03,\n",
       "         5.20288119e-03,   6.86743461e-03,   2.55128958e-02,\n",
       "         9.44089084e-03,   1.24652629e-02,   5.94105094e-03,\n",
       "         7.33460270e-03,   3.53511403e-03,   2.12112760e-02,\n",
       "         7.45547629e-03,   7.21624827e-03,   1.61759300e-02,\n",
       "         4.56612154e-03,   4.83645116e-03,   5.46781939e-03,\n",
       "         2.92141844e-03,   1.45282586e-02,   8.62160740e-03,\n",
       "         5.74177140e-03,   8.58723429e-03,   3.59263345e-03,\n",
       "         5.05894842e-02,   9.80176985e-03,   9.25325020e-03,\n",
       "         4.16320394e-03,   3.48283388e-03,   2.92004833e-03,\n",
       "         1.79637685e-03,   4.99579066e-03,   4.08825505e-03,\n",
       "         4.68035624e-03,   4.64695739e-03,   1.30666305e-02,\n",
       "         7.20159762e-03,   1.26107168e-02,   5.51976999e-03,\n",
       "         3.81657111e-03,   4.46774877e-03,   8.62454148e-03,\n",
       "         3.48611719e-03,   4.36238311e-03,   0.00000000e+00,\n",
       "         1.08840221e-02,   7.81253368e-03,   2.88054668e-03,\n",
       "         3.24497485e-03,   4.68209260e-03,   1.25468295e-02,\n",
       "         4.27241955e-03,   5.43537072e-03,   5.01050213e-03,\n",
       "         2.58272191e-03,   0.00000000e+00,   6.58018941e-03,\n",
       "         8.89479165e-03,   8.68285674e-03,   2.28067307e-02,\n",
       "         8.27355073e-03,   3.30521661e-03,   6.15343009e-03,\n",
       "         3.25435772e-03,   7.98524304e-03,   0.00000000e+00,\n",
       "         9.32272268e-03,   2.33830213e-03,   3.08646002e-03,\n",
       "         4.46612856e-03,   4.10240614e-03,   9.79182028e-03,\n",
       "         4.75514728e-03,   1.15928803e-02,   3.39444361e-03,\n",
       "         7.74006533e-03,   4.99595989e-03,   5.50687339e-03,\n",
       "         4.76575041e-03,   5.89213406e-03,   6.18893303e-03,\n",
       "         4.38254911e-03,   3.71107157e-03,   9.21828448e-03,\n",
       "         1.99927702e-02,   1.24437138e-02,   5.40319741e-03,\n",
       "         4.69063006e-03,   5.82308167e-03,   1.06818110e-02,\n",
       "         6.77037723e-03,   1.11588171e-02,   4.10524752e-03,\n",
       "         2.53732566e-02,   0.00000000e+00,   8.40754332e-03,\n",
       "         1.06423523e-02,   6.81113636e-03,   6.28393472e-03,\n",
       "         6.40752140e-03,   3.99991575e-03,   8.45358232e-03,\n",
       "         2.69084807e-03,   7.17962584e-03,   2.88813244e-03,\n",
       "         3.22626892e-03,   1.64422245e-02,   6.90450033e-03,\n",
       "         5.31772306e-03,   4.33495208e-03,   5.20496451e-03,\n",
       "         5.31223197e-03,   2.09451818e-03,   5.21185086e-03,\n",
       "         0.00000000e+00,   0.00000000e+00,   4.60623226e-03,\n",
       "         7.16352752e-03,   4.71239235e-03,   4.19713172e-03,\n",
       "         1.13414979e-02,   1.10215260e-02,   3.58318723e-03,\n",
       "         4.82645817e-03,   5.71363116e-03,   3.20274305e-03,\n",
       "         3.67043184e-03,   2.95276218e-03,   1.53764955e-02,\n",
       "         1.21505317e-02,   0.00000000e+00,   8.44312655e-03,\n",
       "         8.07038433e-03,   8.19803747e-03,   6.17816478e-03,\n",
       "         3.67872775e-03,   5.45830634e-03,   4.01739549e-03,\n",
       "         6.92330822e-03,   6.00690557e-03,   1.20447051e-02,\n",
       "         6.99427018e-03,   9.33656701e-04,   5.48159266e-03,\n",
       "         4.51317086e-03,   4.24999634e-03,   3.29248193e-03,\n",
       "         5.18921791e-03,   1.04820509e-02,   3.58633608e-03,\n",
       "         3.86829757e-03,   1.06878849e-02,   1.28973296e-02,\n",
       "         9.16292077e-03,   7.46332697e-03,   1.33499876e-02,\n",
       "         7.13768522e-03,   4.73206483e-03,   3.30169170e-03,\n",
       "         3.20822680e-03,   5.43695220e-03,   8.27737389e-03,\n",
       "         1.33793809e-02,   7.13664643e-03,   3.92137367e-03,\n",
       "         8.95892711e-03,   3.11387306e-02,   5.15779700e-03,\n",
       "         7.46088032e-03,   4.98160349e-03,   3.03831731e-03,\n",
       "         6.43896622e-03,   2.90957959e-03,   7.24013357e-03,\n",
       "         7.78012436e-03,   9.78086860e-03,   3.56694335e-03,\n",
       "         5.09382383e-03,   8.02014789e-03,   2.20946423e-02,\n",
       "         6.05654219e-03,   1.12631888e-02,   2.01429374e-02,\n",
       "         4.44747659e-03,   3.86705477e-03,   0.00000000e+00,\n",
       "         5.30487385e-03,   7.24767766e-03,   1.29180424e-02,\n",
       "         3.43119118e-03,   7.42277195e-03,   2.14381257e-02,\n",
       "         6.66811202e-03,   0.00000000e+00,   2.25234444e-02,\n",
       "         2.68047033e-03,   5.82747641e-03,   8.78039082e-03,\n",
       "         6.56357514e-03,   4.00561032e-03,   9.97373325e-03,\n",
       "         0.00000000e+00,   8.38092050e-03,   1.03490892e-02,\n",
       "         1.24657495e-02,   3.13178851e-03,   1.22931475e-02,\n",
       "         5.97259966e-03,   9.74559219e-03,   7.26559086e-03,\n",
       "         1.70573931e-03,   9.78892911e-03,   8.14274235e-03,\n",
       "         7.27923938e-03,   6.71107237e-03,   4.65509210e-03,\n",
       "         4.55368300e-03,   0.00000000e+00,   6.21029681e-03,\n",
       "         4.25418231e-02,   3.06772817e-03,   4.36159998e-03,\n",
       "         1.44274921e-02,   0.00000000e+00,   6.14151881e-03,\n",
       "         4.71494512e-03,   6.05910079e-03,   3.15702824e-03,\n",
       "         3.40144420e-03,   8.96246283e-03,   2.50604097e-02,\n",
       "         6.18236226e-03,   3.15291924e-03,   7.23047998e-03,\n",
       "         9.64289674e-04,   6.91629578e-03,   2.54867006e-03,\n",
       "         4.91071530e-03,   9.67100846e-03,   5.31980651e-03,\n",
       "         5.78536508e-03,   7.96388631e-03,   4.17097795e-03,\n",
       "         3.88614307e-03,   0.00000000e+00,   3.05560364e-03,\n",
       "         4.92939634e-03,   3.73973206e-03,   3.43816651e-03,\n",
       "         8.76655422e-03,   2.58179761e-03,   5.26702211e-03,\n",
       "         1.13565764e-02,   2.99060195e-03,   5.33919261e-03,\n",
       "         1.48626116e-02,   1.51249165e-02,   4.34899063e-03,\n",
       "         4.56535124e-03,   1.05683258e-02,   7.63068314e-03,\n",
       "         0.00000000e+00,   6.82787927e-03,   6.65972540e-03,\n",
       "         1.70277151e-03,   1.22032386e-02,   5.07663746e-03,\n",
       "         8.13627661e-03,   4.33779642e-03,   5.64217235e-03,\n",
       "         3.18263799e-03,   8.97422833e-03,   5.09037997e-03,\n",
       "         1.31873667e-02,   4.29041709e-03,   2.47029394e-02,\n",
       "         1.47168320e-02,   2.02622136e-02,   3.66409199e-03,\n",
       "         5.33562788e-03,   0.00000000e+00,   3.37638511e-03,\n",
       "         5.00212086e-03,   6.78996950e-03,   0.00000000e+00,\n",
       "         1.75868234e-03,   5.84088277e-03,   1.05575538e-02,\n",
       "         0.00000000e+00,   0.00000000e+00,   5.34448802e-03,\n",
       "         1.03181229e-02,   9.62955215e-03,   4.50774729e-03,\n",
       "         8.27753524e-03,   1.49388375e-02,   1.54133897e-02,\n",
       "         3.27752323e-03,   5.55592718e-03,   9.68219690e-03,\n",
       "         7.65251995e-03,   2.87850642e-02,   3.42488541e-03,\n",
       "         1.21773593e-02,   6.08434569e-03,   7.41796991e-03,\n",
       "         7.86587847e-03,   4.26646433e-03,   7.48130160e-05,\n",
       "         4.63820581e-03,   4.84287460e-03,   1.25429947e-02,\n",
       "         3.57089690e-03,   5.83328995e-03,   3.49924212e-03,\n",
       "         6.95848492e-03,   3.93382158e-03,   1.17909229e-02,\n",
       "         2.33831866e-02,   7.78614844e-03,   5.53110720e-03,\n",
       "         0.00000000e+00,   8.38849410e-03,   2.92430548e-03,\n",
       "         1.18628020e-02,   1.00408402e-02,   1.74580696e-02,\n",
       "         4.63859435e-03,   5.67453282e-03,   3.13276611e-03,\n",
       "         6.99180670e-03,   4.72310835e-03,   3.87987370e-03,\n",
       "         1.29201010e-02,   4.90734437e-03,   7.75595524e-03,\n",
       "         1.03218653e-02,   2.50007780e-03,   6.94488306e-03,\n",
       "         5.67886172e-03,   4.05753352e-03,   1.60990426e-02,\n",
       "        -4.79897013e-04,   4.93753342e-03,   4.86141809e-03,\n",
       "         8.66943991e-03,   1.26004535e-02,   2.80872835e-03,\n",
       "         2.98532909e-03,   7.83639434e-03,   6.82413783e-03,\n",
       "         4.83241524e-03,   4.77176742e-03,   8.99861671e-03,\n",
       "         1.16185160e-02,   1.57225431e-02,   1.25689498e-02,\n",
       "         8.23324743e-03,   3.93832450e-03,   3.87829825e-03,\n",
       "         2.91178281e-03,   1.04332106e-02,   5.90834483e-03,\n",
       "         2.23125161e-02,   1.75199750e-02,   2.95584767e-03,\n",
       "         1.01528364e-02])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据用户对item的预测打分产生top推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Sort the indices of user_item_scores based upon their value\n",
    "#Also maintain the corresponding score\n",
    "sort_index = sorted(((e,i) for i,e in enumerate(list(user_items_scores))), reverse=True)\n",
    "    \n",
    "#Create a dataframe from the following\n",
    "columns = ['user_id', 'item', 'score', 'rank']\n",
    "df = pd.DataFrame(columns=columns)\n",
    "         \n",
    "#Fill the dataframe with top 20 item based recommendations\n",
    "rank = 1 \n",
    "for i in range(0,len(sort_index)):\n",
    "    cur_item_index = sort_index[i][1] \n",
    "    cur_item = list (item_index.keys()) [list (item_index.values()).index (cur_item_index)]\n",
    "            \n",
    "    if ~np.isnan(sort_index[i][0]) and cur_item_index not in cur_user_items and rank <= 20:\n",
    "        df.loc[len(df)]=[cur_user,cur_item,sort_index[i][0],rank]\n",
    "        rank = rank+1       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOZPMJT12AAF3B40D1</td>\n",
       "      <td>0.067656</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOACBLB12AB01871C7</td>\n",
       "      <td>0.058425</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOJSXJY12A8C13E32E</td>\n",
       "      <td>0.056707</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOGBOQX12A8C139DF7</td>\n",
       "      <td>0.050589</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SONXUQR12AB0186C9F</td>\n",
       "      <td>0.048701</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVEDOZ12AB01873A1</td>\n",
       "      <td>0.042542</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUWZPO12A6D4F83E3</td>\n",
       "      <td>0.037357</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOELRWA12A8C142D0A</td>\n",
       "      <td>0.033743</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVMADB12A8C137B96</td>\n",
       "      <td>0.032486</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOANOQW12A58A793D2</td>\n",
       "      <td>0.031139</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOSGEGU12AB018188A</td>\n",
       "      <td>0.030390</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOXQIUR12A8AE4654A</td>\n",
       "      <td>0.029832</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOCBSZW12AB01891C1</td>\n",
       "      <td>0.029224</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAJFTU12A6701E0A3</td>\n",
       "      <td>0.028785</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAOSDF12A58A779F1</td>\n",
       "      <td>0.028770</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOGSDHY12AB017BF39</td>\n",
       "      <td>0.028297</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVAGPG12AB0189963</td>\n",
       "      <td>0.026287</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAKPMX12AB018193B</td>\n",
       "      <td>0.025547</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOWKKVL12A8C134D6B</td>\n",
       "      <td>0.025513</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOWEFXC12A6D4FA230</td>\n",
       "      <td>0.025512</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     user_id                item     score  \\\n",
       "0   5a905f000fc1ff3df7ca807d57edb608863db05d  SOZPMJT12AAF3B40D1  0.067656   \n",
       "1   5a905f000fc1ff3df7ca807d57edb608863db05d  SOACBLB12AB01871C7  0.058425   \n",
       "2   5a905f000fc1ff3df7ca807d57edb608863db05d  SOJSXJY12A8C13E32E  0.056707   \n",
       "3   5a905f000fc1ff3df7ca807d57edb608863db05d  SOGBOQX12A8C139DF7  0.050589   \n",
       "4   5a905f000fc1ff3df7ca807d57edb608863db05d  SONXUQR12AB0186C9F  0.048701   \n",
       "5   5a905f000fc1ff3df7ca807d57edb608863db05d  SOVEDOZ12AB01873A1  0.042542   \n",
       "6   5a905f000fc1ff3df7ca807d57edb608863db05d  SOUWZPO12A6D4F83E3  0.037357   \n",
       "7   5a905f000fc1ff3df7ca807d57edb608863db05d  SOELRWA12A8C142D0A  0.033743   \n",
       "8   5a905f000fc1ff3df7ca807d57edb608863db05d  SOVMADB12A8C137B96  0.032486   \n",
       "9   5a905f000fc1ff3df7ca807d57edb608863db05d  SOANOQW12A58A793D2  0.031139   \n",
       "10  5a905f000fc1ff3df7ca807d57edb608863db05d  SOSGEGU12AB018188A  0.030390   \n",
       "11  5a905f000fc1ff3df7ca807d57edb608863db05d  SOXQIUR12A8AE4654A  0.029832   \n",
       "12  5a905f000fc1ff3df7ca807d57edb608863db05d  SOCBSZW12AB01891C1  0.029224   \n",
       "13  5a905f000fc1ff3df7ca807d57edb608863db05d  SOAJFTU12A6701E0A3  0.028785   \n",
       "14  5a905f000fc1ff3df7ca807d57edb608863db05d  SOAOSDF12A58A779F1  0.028770   \n",
       "15  5a905f000fc1ff3df7ca807d57edb608863db05d  SOGSDHY12AB017BF39  0.028297   \n",
       "16  5a905f000fc1ff3df7ca807d57edb608863db05d  SOVAGPG12AB0189963  0.026287   \n",
       "17  5a905f000fc1ff3df7ca807d57edb608863db05d  SOAKPMX12AB018193B  0.025547   \n",
       "18  5a905f000fc1ff3df7ca807d57edb608863db05d  SOWKKVL12A8C134D6B  0.025513   \n",
       "19  5a905f000fc1ff3df7ca807d57edb608863db05d  SOWEFXC12A6D4FA230  0.025512   \n",
       "\n",
       "   rank  \n",
       "0     1  \n",
       "1     2  \n",
       "2     3  \n",
       "3     4  \n",
       "4     5  \n",
       "5     6  \n",
       "6     7  \n",
       "7     8  \n",
       "8     9  \n",
       "9    10  \n",
       "10   11  \n",
       "11   12  \n",
       "12   13  \n",
       "13   14  \n",
       "14   15  \n",
       "15   16  \n",
       "16   17  \n",
       "17   18  \n",
       "18   19  \n",
       "19   20  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
